import { LayerType } from "./ObstacleConfig";
import SquareItem from "./SquareItem";

import n from "./EventMgr"
import l from "./MatchGameEventType"
import s from "./frameworkManager"
import c from "./EngineUtil"
import u from "./MatchGameData"
import p from "./MatchGameConfig"
import d from "./ObstacleConfig"
import h from "./MatchGameDataMgr"
import f from "./SquareStatusMgr"
import g from "./SquareItem"
import y from "./matchItemPrefab"
import m from "./MatchAniCtrl"
import _, { GuideElimitateType } from "./MatchGameGuidePageCtrl"
import v from "./MatchGameCenterCtrl"
import MatchGameConfig from "./MatchGameConfig";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MatchItemPrefabCtrl extends SquareItem {

    _startPos = null;
    _moveOff = false;
    spine = null;
    guideAni = null;

    onLoad() {
        this.onUILoad();
        this.addButtonListen();
        this.addEvent();
    }
    public ui;
    onUILoad() {
        this.ui = this.node.addComponent(y);
    }

    addEvent() { }

    addButtonListen() { }

    private _couldTouch;
    init(t, a, o) {
        this.ui.lightSpine.active = false;
        super.init(t, a, p.SquareType.MatchItem, o, null);
        this.layerType = LayerType.Middle;
        this._couldTouch = true;
        this.spine = this.ui.spine.getComponent(sp.Skeleton);
        var i = m.setMatchAniSkin(o);
        try {
            this.spine.setSkin(i);
        } catch (e) {
            s.error("skinName==", i);
        }
        m.playIdleAni(this.spine);

        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStartEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEndEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEndEvent, this);
    }

    touchStartEvent(e?) {
        if (
            !v._instance.existSquareUpperLayer(this._row, this._col, LayerType.Middle) &&
            this._couldTouch &&
            f.getSquareNormalStatus(this)
        ) {
            this._startPos = e.getLocation();
            this._moveOff = true;
            return true;
        }
    }

    touchMoveEvent(e) {
        if (
            !v._instance.existSquareUpperLayer(this._row, this._col, LayerType.Middle) &&
            this._couldTouch &&
            this._moveOff &&
            f.getSquareNormalStatus(this)
        ) {
            var t = e.getLocation();
            if (this._startPos) {
                var a = Math.abs(t.x - this._startPos.x),
                    o = Math.abs(t.y - this._startPos.y);
                if (u.duringGuide) {
                    if (
                        (this.guideElimitateType == GuideElimitateType.Left ||
                            this.guideElimitateType == GuideElimitateType.Any) &&
                        a > o &&
                        a > MatchGameConfig.ITEM_WIDTH / 2 &&
                        t.x - this._startPos.x < 0
                    ) {
                        n.trigger(l.ON_GUIDE_ELIMITATE);
                        this.setOtherChoose({
                            row: this._row - 1,
                            col: this._col,
                        });
                        return;
                    }
                    if (
                        (this.guideElimitateType == GuideElimitateType.Up ||
                            this.guideElimitateType == GuideElimitateType.Any) &&
                        o > a &&
                        o > MatchGameConfig.ITEM_HEIGHT / 2 &&
                        t.y - this._startPos.y > 0
                    ) {
                        n.trigger(l.ON_GUIDE_ELIMITATE);
                        this.setOtherChoose({
                            row: this._row,
                            col: this._col - 1,
                        });
                        return;
                    }
                    if (
                        (this.guideElimitateType == GuideElimitateType.Right ||
                            this.guideElimitateType == GuideElimitateType.Any) &&
                        a > o &&
                        a > MatchGameConfig.ITEM_WIDTH / 2 &&
                        t.x - this._startPos.x > 0
                    ) {
                        n.trigger(l.ON_GUIDE_ELIMITATE);
                        this.setOtherChoose({
                            row: this._row + 1,
                            col: this._col,
                        });
                        return;
                    }
                    if (
                        (this.guideElimitateType == GuideElimitateType.Down ||
                            this.guideElimitateType == GuideElimitateType.Any) &&
                        o > a &&
                        o > MatchGameConfig.ITEM_HEIGHT / 2 &&
                        t.y - this._startPos.y < 0
                    ) {
                        n.trigger(l.ON_GUIDE_ELIMITATE);
                        this.setOtherChoose({
                            row: this._row,
                            col: this._col + 1,
                        });
                        return;
                    }
                } else if (a > o && a > MatchGameConfig.ITEM_WIDTH / 2 && t.x - this._startPos.x < 0) {
                    this.setOtherChoose({
                        row: this._row - 1,
                        col: this._col,
                    });
                } else if (o > a && o > MatchGameConfig.ITEM_HEIGHT / 2 && t.y - this._startPos.y > 0) {
                    this.setOtherChoose({
                        row: this._row,
                        col: this._col - 1,
                    });
                } else if (a > o && a > MatchGameConfig.ITEM_WIDTH / 2 && t.x - this._startPos.x > 0) {
                    this.setOtherChoose({
                        row: this._row + 1,
                        col: this._col,
                    });
                } else if (o > a && o > MatchGameConfig.ITEM_HEIGHT / 2 && t.y - this._startPos.y < 0) {
                    this.setOtherChoose({
                        row: this._row,
                        col: this._col + 1,
                    });
                }
            }
        }
    }

    touchEndEvent() {
        s.log("prop touchEnd", this._moveOff);
        this._moveOff = true;
    }

    setChoose() { }

    setOtherChoose(e) {
        var t = this;
        if (
            this._couldTouch &&
            this._moveOff &&
            !v._instance.existSquareUpperLayer(e.row, e.col, LayerType.Middle) &&
            h.middleSquareGrid[e.row][e.col]?.canMove
        ) {
            if (
                !v._instance.displayMapInfo[e.row] ||
                v._instance.displayMapInfo[e.row][e.col] != p.DisplayMapType.Normal ||
                h.middleSquareGrid[e.row][e.col]
            ) {
                if (
                    h.middleSquareGrid[e.row] &&
                    h.middleSquareGrid[e.row][e.col] &&
                    f.getSquareNormalStatus(this) &&
                    f.getSquareNormalStatus(h.middleSquareGrid[e.row][e.col])
                ) {
                    this._moveOff = false;
                    if (u.duringGuide) {
                        this.scheduleOnce(() => {
                            h.changeMatchAndOthers(
                                {
                                    i: t._row,
                                    j: t._col,
                                },
                                {
                                    i: e.row,
                                    j: e.col,
                                }
                            );
                            n.trigger(l.GUIDE_ELIMINATE);
                        });
                    } else {
                        h.changeMatchAndOthers(
                            {
                                i: this._row,
                                j: this._col,
                            },
                            {
                                i: e.row,
                                j: e.col,
                            }
                        );
                    }
                } else {
                    this._moveOff = false;
                    this.touchWall(e);
                }
            } else {
                this._moveOff = false;
                h.changeSquareAndEmpty(
                    {
                        i: this._row,
                        j: this._col,
                    },
                    {
                        i: e.row,
                        j: e.col,
                    }
                );
            }
        }
    }

    touchWall(e) {
        var t = this,
            a = c.GetChildByName(this.node, "spine", true),
            o = 5 * -(this._row - e.row),
            i = 5 * (this._col - e.col),
            r = 7 * -(this._row - e.row),
            n = 7 * (this._col - e.col),
            l = 9 * -(this._row - e.row),
            u = 9 * (this._col - e.col),
            p = cc.v3(a.x, a.y),
            d = cc.v3(a.x + o, a.y + i),
            h = cc.v3(a.x + r, a.y + n),
            f = cc.v3(a.x + l, a.y + u),
            g = 0 == o
                ? {
                    scaleX: 0.93,
                    scaleY: 1.027,
                    position: f,
                }
                : {
                    scaleX: 1.027,
                    scaleY: 0.93,
                    position: f,
                };
        s.log("touchWall", o, i);
        // this.node.zIndex = 999;
        cc.tween(a)
            .to(0.05, {
                scaleX: 0 !== o ? 1 : 1.02,
                scaleY: 0 !== i ? 1 : 1.02,
                position: d,
            })
            .to(0.03, {
                scaleX: 0 !== o ? 1 : 0.958,
                scaleY: 0 !== i ? 1 : 0.958,
                position: h,
            })
            .to(0.05, g)
            .to(
                0.07,
                {
                    scaleX: 1,
                    scaleY: 1,
                    position: p,
                },
                {
                    easing: "backOut",
                }
            )
            .call(() => {
                a.scaleX = 1;
                a.scaleY = 1;
                a.position = p;
                t.node.zIndex = 0;
            })
            .start();
    }

    clear() {
        this.node.off(cc.Node.EventType.TOUCH_START, this.touchStartEvent, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchEndEvent, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.touchEndEvent, this);
    }

    moveTo(t, a, o) {
        super.moveTo(t, a, o);
    }

    hideItem() {
        var t = this;
        super.hideItem();
        m.playDestoryAni(this, {
            row: this._row,
            col: this._col,
        });
        m.playMatchRemoveAni(this.spine,  () => {
            t.node.opacity = 0;
        });
    }

    destorySelf(t?) {
        var a = this;
        t === void 0 && (t = false);
        super.destorySelf();
        this.setGuideNormal();
        return new Promise( (e) => {
            if (a.isHide) {
                e(null);
                a.clear();
            } else {
                m.playDestoryAni(a, {
                    row: a._row,
                    col: a._col,
                });
                m.playMatchRemoveAni(a.spine,  () => {
                    e(null);
                    a.clear();
                });
            }
        });
    }

    playLightAni() {
        this.node.angle = 0;
        cc.tween(this.node)
            .to(0.2, {
                angle: 10,
            })
            .to(0.2, {
                angle: 0,
            })
            .to(0.2, {
                angle: -10,
            })
            .to(0.2, {
                angle: 0,
            })
            .start()
            .repeat(5);
    }

    playLightingAni() {
        m.playLightingAni(this.spine);
        this.ui.lightSpine.active = true;
    }

    onGuide() {
        this.ui.spine.getComponent(sp.Skeleton);
    }

    finishGuide() { }

    static prefabUrl = "assets/resources/prefabs/MatchGame/matchItemPrefab";
    static className = "MatchItemPrefabCtrl";
}